﻿// -----------------------------------------------------------------------
// <copyright file="Location_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Location_DAL  :BaseDAL
    {
        // Lấy tất cả Location
        public static List<Location> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Locations);
            return DB.Locations.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy Location theo Id
        public static Location GetDataById(int id)
        {
            return DB.Locations.Where(m => m.Location_ID == id).FirstOrDefault();
        }
        //Thêm một Location
        public static bool Insert(Location Location)
        {
            try
            {
                DB.Locations.InsertOnSubmit(Location);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Location
        public static bool Delete(Location Location)
        {
            try
            {
                DB.Locations.DeleteOnSubmit(Location);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Location
        public static bool Update(Location Location)
        {
            try
            {
                //Location objLocation = GetDataById(Location.Location_ID);
                //objLocation.CodeLocation = Location.CodeLocation;
                //objLocation.LocationName = Location.LocationName;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
      
        // Kiểm tra xem Location Code có tồn tại chưa ?
        public static bool IsExits(Location Location)
        {
            Location objLocation = null;
            objLocation = DB.Locations.Where(m => (m.CodeLocation == Location.CodeLocation) &&
                (m.Location_ID != Location.Location_ID) && (m.Ship_ID == Location.Ship_ID)).FirstOrDefault();
            return objLocation != null ? true : false;
        }
        public static bool IsRelationship(Location location)
        {
            Equipment equipment = DB.Equipments.Where(n => n.Location_ID == location.Location_ID).FirstOrDefault();
            if (null != equipment)
                return false;

            return true;
        }

    }
}
